package com.daykw.douyin.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;

import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.http.HttpBase;
import com.daykw.douyin.model.VideoInfo;
import com.daykw.douyin.model.VideoTask;
import com.daykw.douyin.utils.SpringContextHolder;

public class VideoECThread extends Thread {

	private static Logger logger = Logger.getLogger(VideoECThread.class);

	@Override
	public void run() {

		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

			List<VideoTask> tasks = dao.query(VideoTask.class, Cnd.where("ec_status", "=", 0).limit(1, 10));

			HttpBase http = new HttpBase();
			Map<String, String> headers = new HashMap<String, String>();
			String cookie = dao.fetch("t_cookie", Cnd.where("name", "=", "13235801201")).getString("cookie");

			if (StringUtils.isEmpty(cookie)) {

				logger.info("cookie:13235801201没了...");
				return;
			}

			for (VideoTask task : tasks) {

				VideoInfo video = dao.fetch(VideoInfo.class, Cnd.where("kw_id", "=", task.getKw_id()));

				Map<String, String> data = new HashMap<String, String>();
				data.put("promotionId", video.getKw_product_id());
				data.put("id", video.getDataid());
				data.put("awemeid", video.getKw_id());
				headers.put("Cookie", cookie);

				try {
					String ehtml = http.post("https://dy.feigua.cn/Aweme/GetEcommerceAnalysis", headers, data);

					if (!ehtml.equals("fail")) {
						Document edoc = Jsoup.parse(ehtml);
						String eimg = edoc.select("div.details-hd div.details-hd-cover img").attr("src");
						String ename = edoc.select("div.details-hd div.details-info dt").first().ownText();
						String ehost = edoc.select("div.details-hd div.details-info dt span").text();
						String enew_price = edoc.select("div.details-hd div.details-info dd.price-box span.price")
								.text();
						String eold_price = edoc.select("div.details-hd div.details-info dd.price-box span.original")
								.text();
						Elements statisticals = edoc.select("div.details-hd div.details-info dd.statistical");
						for (Element es : statisticals) {
							String txt = es.text();
							if (txt.indexOf("抖音浏览量") >= 0) {
								// 抖音浏览量：41.3w
								String view = txt.replace("抖音浏览量：", "");
								video.setProduct_view(view);
							} else if (txt.indexOf("全网销量") >= 0) {
								// 全网销量：2895件
								String sales = txt.replace("全网销量：", "");
								video.setProduct_sales(sales);

							}
						}
						video.setProduct_img(eimg);
						video.setProduct_name(ename);
						video.setProduct_new_price(enew_price);
						video.setProduct_old_price(eold_price);
						video.setKw_product_host(ehost);
						dao.updateIgnoreNull(video);

						dao.update(VideoTask.class, Chain.make("ec_status", 2),
								Cnd.where("kw_id", "=", video.getKw_id()));
					}
					logger.info("##########" + task.getKw_id() + "电商信息抓完，暂停3秒");
					Thread.sleep(3000);
				} catch (Exception ee) {
					logger.error("", ee);
					dao.update(VideoTask.class, Chain.make("ec_status", 3), Cnd.where("kw_id", "=", video.getKw_id()));
				}

			}

		} catch (Exception e) {

			logger.error("", e);
		}

	}
}
